#include <connectivity/uart.h>
#include <bsp.h>
#include <sheriffos.h>

#if BSP_HAS_UART7 == 1
extern UART_HandleTypeDef huart7;
os::Uart* Instance_UART7;
#endif

#if BSP_HAS_UART8 == 1
extern UART_HandleTypeDef huart8;
os::Uart* Instance_UART8;
#endif

#if BSP_HAS_UART9 == 1
extern UART_HandleTypeDef huart9;
os::Uart* Instance_UART9;
#endif

#if BSP_HAS_USART1 == 1
extern UART_HandleTypeDef huart1;
os::Uart* Instance_USART1;
#endif

#if BSP_HAS_USART2 == 1
extern UART_HandleTypeDef huart2;
os::Uart* Instance_USART2;
#endif

#if BSP_HAS_USART3 == 1
extern UART_HandleTypeDef huart3;
os::Uart* Instance_USART3;
#endif

#if BSP_HAS_USART6 == 1
extern UART_HandleTypeDef huart6;
os::Uart* Instance_USART6;
#endif

#if BSP_HAS_USART10 == 1
extern UART_HandleTypeDef huart10;
os::Uart* Instance_USART10;
#endif

namespace os {
Uart& Uart::Instance(USART_TypeDef* usart) {
#if BSP_HAS_USART1 == 1
    if (usart == USART1) {
        static Uart instance(&huart1);
        Instance_USART1 = &instance;
        return instance;
    }
#endif

#if BSP_HAS_USART2 == 1
    if (usart == USART2) {
        static Uart instance(&huart2);
        Instance_USART2 = &instance;
        return instance;
    }
#endif

#if BSP_HAS_USART3 == 1
    if (usart == USART3) {
        static Uart instance(&huart3);
        Instance_USART3 = &instance;
        return instance;
    }
#endif

#if BSP_HAS_USART6 == 1
  if (usart == USART6) {
    static Uart instance(&huart6);
    Instance_USART6 = &instance;
    return instance;
  }
#endif

#if BSP_HAS_USART10 == 1
  if (usart == USART10) {
    static Uart instance(&huart10);
    Instance_USART10 = &instance;
    return instance;
  }
#endif

#if BSP_HAS_UART7 == 1
  if (usart == UART7) {
    static Uart instance(&huart7);
    Instance_UART7 = &instance;
    return instance;
  }
#endif

#if BSP_HAS_UART9 == 1
  if (usart == UART9) {
    static Uart instance(&huart9);
    Instance_UART9 = &instance;
    return instance;
  }
#endif

    osError("Get Uart Instance Err");
}

UART_HandleTypeDef* Uart::GetHandlePtr() { return handle_; }
} // namespace os